package com.lzx.controller;



import com.alibaba.fastjson.JSONObject;
import com.lzx.model.FileEntity;

import com.lzx.model.zlzs.*;
import com.lzx.service.FileEntityService;
import com.lzx.service.zlzs.*;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("")
public class ZlzsController {
    @Autowired
    CommodityService commodityService;
    @Autowired
    CompanyService companyService;
    @Autowired
    FertilizerService fertilizerService;
    @Autowired
    DistributionService distributionService;
    @Autowired
    MachiningService machiningService;
    @Autowired
    PhotoService photoService;
    @Autowired
    GerminationService germinationService;
    @Autowired
    private FileEntityService fileBeanService;

    @RequestMapping("/zlzs.html")
    public ModelAndView indexzlzs(String id) {

        if (id == null) {
            id = "1234341";
        }
        String companyid = "";
       // System.out.println("commodityid--------" + id);
        ModelAndView mv = new ModelAndView("/system/zlzs");
        //查询商品
        try {
            Commodity commodity = commodityService.selectdemobyid(id);
            companyid = commodity.getEnterpriseid();
            mv.addObject("commodity", commodity);

            List<Commodity> list = commodityService.selectdemolist();
            mv.addObject("list", list);

            FileEntity fileEntity = new FileEntity();
            fileEntity.setYwId(id);
            fileEntity.setYwType("commoditytp");
            List<FileEntity> fileBeans = fileBeanService.selectdemolist(fileEntity);
            System.out.println(fileBeans);
            mv.addObject("fileBeans", fileBeans);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //查询企业
        try {
            Company company = companyService.selectdemobyid(companyid);
            mv.addObject("company", company);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //肥料使用
        try {
            Fertilizer fertilizer = fertilizerService.selectdemobyid(id);
            mv.addObject("fertilizer", fertilizer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //配送管理
        try {
            Distribution distribution = distributionService.selectdemobyid(id);
            mv.addObject("distribution", distribution);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //加工管理
        try {
            Machining machining=machiningService.selectdemobyid(id);
            mv.addObject("machining", machining);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //选种催芽
        try {
            Germination germination=germinationService.selectdemobyid(id);
            mv.addObject("germination", germination);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //全生长期图片
        Photo photo=new Photo();
        photo.setPremarks("1");
        photo.setCommodityid(id);
        List ids=new ArrayList();
        List<FileEntity> tps=new ArrayList<>();
        try {
            List<Photo>  list =photoService.selectdemolistyue(photo);
            if(list.size()>0){
                for(Photo po:list){
                    ids.add(po.getId());
                }
            }
            //System.out.println("ids------------"+ids.toString());
            for(int i=0;i<ids.size();i++) {
                FileEntity file = new FileEntity();
                file.setYwId((String) ids.get(i));
                file.setYwType("qszqtp");
                List<FileEntity> fileBeans = fileBeanService.selectdemolist(file);
                if(fileBeans.size()>0){
                    for(FileEntity fi:fileBeans){
                        tps.add(fi);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        mv.addObject("tps", tps);

        return mv;
    }

    @RequestMapping("/zlzsqszqtp.html")
    public String lsjg(String commodityid,String yue) {
       // System.out.println("commodityid-----"+commodityid);
       // System.out.println("yue-----"+yue);
        Photo photo=new Photo();
        photo.setPremarks(yue);
        photo.setCommodityid(commodityid);
        List ids=new ArrayList();
        List<FileEntity> tps=new ArrayList<>();
        try {
            List<Photo>  list =photoService.selectdemolistyue(photo);
            if(list.size()>0){
                for(Photo po:list){
                    ids.add(po.getId());
                }
            }
            System.out.println("ids------------"+ids.toString());
            for(int i=0;i<ids.size();i++) {
                FileEntity file = new FileEntity();
                file.setYwId((String) ids.get(i));
                file.setYwType("qszqtp");
                List<FileEntity> fileBeans = fileBeanService.selectdemolist(file);
                if(fileBeans.size()>0){
                    for(FileEntity fi:fileBeans){
                        tps.add(fi);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonstr = new JSONObject();
        jsonstr.put("tps", tps);
        return jsonstr.toJSONString();
    }
}
